import { FACTORY_TYPE_OPTIONS, FIT_HEAT_OPTIONS, PRICE_BAND_OPTIONS, THICKNESS_OPTIONS } from '@/constant/goods'
import { CooperateStatusOptions } from '@/constant/supplier'
import { TwoDateVerify, validateOutPackageNum } from '@/utils/validator'
import { PlusOutlined } from '@ant-design/icons'
import { Button, Empty, FormInstance, Image } from 'antd'
import { IOptionClass } from '../Plan'
import { ISkuType } from './types'

export const useFormBase = ({
  DICTDATA,
  SupplierOption,
  mainSupplierCode,
  detailInfo,
  FormStylebaseinfo,
  handleSearchSupplier,
  SupplierOptionOther,
  handleSearchSupplierOther,
}: {
  DICTDATA: IDICTDATAType
  SupplierOption: IOptionItem[]
  SupplierOptionOther: IOptionItem[]
  handleSearchSupplierOther: (newValue: string) => void
  mainSupplierCode: string | number
  detailInfo: Record<string, any>
  FormStylebaseinfo: FormInstance
  handleSearchSupplier: (newValue: string) => void
}): IItemArray[] => {
  const SupplierOptionNotMain = SupplierOptionOther?.filter((item) => item.value != mainSupplierCode) || []
  return [
    {
      name: 'spuName',
      label: '款式名称',
      type: 'Input',
      placeholder: '请输入款式名称',
      rules: [
        {
          required: true,
          message: '请输入款式名称',
        },
      ],
    },
    {
      name: 'mainSupplierCode',
      label: '主供应商',
      type: 'Select',
      option: SupplierOption,
      placeholder: '请选择主供应商',
      rules: [
        {
          required: true,
          message: '请选择主供应商',
        },
      ],
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        filterOption: false,
        onSearch: handleSearchSupplier,
        onChange: (val: string) => {
          const otherSupplierCodeList: string[] = FormStylebaseinfo.getFieldValue('otherSupplierCodeList')
          if (otherSupplierCodeList && otherSupplierCodeList.length > 0 && val) {
            const filterCodes = otherSupplierCodeList?.filter((it) => it != val)
            FormStylebaseinfo.setFieldValue('otherSupplierCodeList', filterCodes)
          }
        },
      },
    },
    {
      name: 'factoryTypeCode',
      label: '工厂类型',
      type: 'Select',
      option: FACTORY_TYPE_OPTIONS,
      placeholder: '请选择工厂类型',
      rules: [
        {
          required: true,
          message: '请选择工厂类型',
        },
      ],
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'otherSupplierCodeList',
      label: '其他可生产供应商',
      type: 'Select',
      option: SupplierOptionNotMain,
      placeholder: '请选择其他可生产供应商',
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        filterOption: false,
        mode: 'multiple',
        maxTagCount: 'responsive',
        onSearch: handleSearchSupplierOther,
      },
    },
    {
      name: 'originSalePrice',
      label: '吊牌价',
      type: 'InputNumber',
      placeholder: '请输入吊牌价',
      config: {
        disabled: true,
        precision: 2,
        min: 0,
        onChange: (val: number) => {
          const res = (((val - FormStylebaseinfo.getFieldValue('spuCost')) / val) * 100).toFixed(2)
          FormStylebaseinfo.setFieldValue('realProfitRate', res)
          if (!val) FormStylebaseinfo.setFieldValue('realProfitRate', '')
        },
      },
    },
    {
      name: 'priceProperty',
      label: '价格属性',
      type: 'Select',
      option: DICTDATA.sales_type,
      placeholder: '请选择价格属性',
      rules: [
        {
          required: true,
          message: '请选择价格属性',
        },
      ],
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'priceAuth',
      label: '定价权限',
      type: 'Select',
      option: DICTDATA.pricing_auth,
      placeholder: '请选择定价权限',
      rules: [
        {
          required: true,
          message: '请选择定价权限',
        },
      ],
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'spuCost',
      label: '款式成本',
      type: 'InputNumber',
      placeholder: '请输入款式成本',
      config: {
        precision: 2,
        min: 0,
        onChange: (val: number) => {
          const res = (
            ((FormStylebaseinfo.getFieldValue('originSalePrice') - val) /
              FormStylebaseinfo.getFieldValue('originSalePrice')) *
            100
          ).toFixed(2)
          FormStylebaseinfo.setFieldValue('realProfitRate', res)
          if (!val) FormStylebaseinfo.setFieldValue('realProfitRate', '')
        },
      },
    },
    {
      name: 'realProfitRate',
      label: '实际毛利率',
      type: 'InputNumber',
      placeholder: '实际毛利率',
      config: {
        disabled: true,
        precision: 2,
        min: 0,
        addonAfter: '%',
      },
    },
    {
      name: 'factorySpuCode',
      label: '厂家款号',
      type: 'Input',
      placeholder: '请输入厂家款号',
      rules: [
        {
          required: true,
          message: '请输入厂家款号',
        },
      ],
    },
    {
      name: 'releaseSeasonDate',
      label: '上市日期',
      type: 'DatePicker',
      placeholder: '请选择上市日期',
      rules: [
        ({ getFieldValue }: any) => ({
          validator: (_: any, value: any) =>
            TwoDateVerify(_, value, getFieldValue('releaseSeasonDate'), getFieldValue('overSeasonDate')),
        }),
      ],
    },
    {
      name: 'overSeasonDate',
      label: '过季日期',
      type: 'DatePicker',
      placeholder: '请选择过季日期',
      rules: [
        ({ getFieldValue }: any) => ({
          validator: (_: any, value: any) =>
            TwoDateVerify(
              _,
              value,
              getFieldValue('releaseSeasonDate'),
              getFieldValue('overSeasonDate'),
              '过季日期需要大于上市日期',
            ),
        }),
      ],
    },
    {
      name: 'seasonGroup',
      label: '季节分组',
      type: 'Select',
      option: DICTDATA.season_classify,
      placeholder: '请选择季节分组',
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'suitAgeGroup',
      label: '适销年龄段',
      type: 'Select',
      option: DICTDATA.age_group,
      placeholder: '请选择适销年龄段',
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'productLevel',
      label: '商品等级',
      type: 'Select',
      option: DICTDATA.product_level,
      placeholder: '请选择商品等级',
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'designerName',
      label: '设计师/买手',
      type: 'Input',
      placeholder: '请输入设计师/买手',
    },
  ]
}
export const useFormtaginfo = ({
  DICTDATA,
  productTemplateVO,
}: {
  DICTDATA: IDICTDATAType
  productTemplateVO: IGoodsTemplateType
}): IItemArray[] => {
  const originConfig = [
    {
      name: 'productSeries',
      label: '商品系列',
      type: 'Input',
      placeholder: '请输入商品系列',
      rules: [
        {
          required: false,
          message: '请输入商品系列',
        },
      ],
      hidden: false,
    },
    {
      name: 'patternType',
      label: '版型',
      type: 'Input',
      placeholder: '请输入版型',
      config: {
        showCount: true,
        maxLength: 50,
      },
      rules: [
        {
          required: false,
          message: '请输入版型',
        },
      ],
      hidden: false,
    },
    {
      name: 'collarType',
      label: '领型',
      type: 'Input',
      placeholder: '请输入领型',
      config: {
        showCount: true,
        maxLength: 50,
      },
      rules: [
        {
          required: false,
          message: '请输入领型',
        },
      ],
      hidden: false,
    },
    {
      name: 'flowerType',
      label: '花型',
      type: 'Input',
      placeholder: '请输入花型',
      config: {
        showCount: true,
        maxLength: 50,
      },
      rules: [
        {
          required: false,
          message: '请输入花型',
        },
      ],
      hidden: false,
    },
    {
      name: 'moduleType',
      label: '杯型',
      type: 'Select',
      option: DICTDATA.module_type,
      placeholder: '请选择杯型',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择杯型',
        },
      ],
      hidden: false,
    },
    {
      name: 'laterThan',
      label: '后比',
      type: 'Select',
      option: DICTDATA.later_than,
      placeholder: '请选择后比',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择后比',
        },
      ],
      hidden: false,
    },
    {
      name: 'moldCupMaterial',
      label: '模杯材质',
      type: 'Input',
      placeholder: '请输入模杯材质',
      config: {
        showCount: true,
        maxLength: 50,
      },
      rules: [
        {
          required: false,
          message: '请输入模杯材质',
        },
      ],
      hidden: false,
    },
    {
      name: 'moldCupThickness',
      label: '模杯厚度',
      type: 'Select',
      option: DICTDATA.mold_cup_thickness,
      placeholder: '请选择模杯厚度',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择模杯厚度',
        },
      ],
      hidden: false,
    },
    {
      name: 'moldCupShape',
      label: '模杯形状',
      type: 'Select',
      option: DICTDATA.mold_cup_shape,
      placeholder: '请选择模杯形状',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择模杯形状',
        },
      ],
      hidden: false,
    },
    {
      name: 'breasted',
      label: '排扣',
      type: 'Select',
      option: DICTDATA.breasted,
      placeholder: '请选择排扣',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择排扣',
        },
      ],
      hidden: false,
    },
    {
      name: 'thicknessCode',
      label: '厚度',
      type: 'Select',
      option: THICKNESS_OPTIONS,
      placeholder: '请选择厚度',
      config: {
        showSearch: true,
        filterOption: true,
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: false,
          message: '请选择厚度',
        },
      ],
      hidden: false,
    },
    {
      name: 'functionType',
      label: '功能',
      type: 'Select',
      option: DICTDATA.function,
      placeholder: '请选择功能',
      config: {
        mode: 'multiple',
        maxTagCount: 'responsive',
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择功能',
        },
      ],
      hidden: false,
    },
    {
      name: 'styleCode',
      label: '风格',
      type: 'Select',
      option: DICTDATA.style,
      placeholder: '请选择风格',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择风格',
        },
      ],
      hidden: false,
    },
    {
      name: 'patternLayoutCode',
      label: '图案布局',
      type: 'Select',
      option: DICTDATA.pattern_layout,
      placeholder: '请输入图案布局',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请输入图案布局',
        },
      ],
      hidden: false,
    },
    {
      name: 'elementCode',
      label: '元素',
      type: 'Select',
      option: DICTDATA.element,
      placeholder: '请输入元素',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请输入元素',
        },
      ],
      hidden: false,
    },
    {
      name: 'technicsCode',
      label: '工艺',
      type: 'Select',
      option: DICTDATA.technics,
      placeholder: '请选择工艺',
      config: {
        showSearch: true,
        filterOption: true,
      },
      rules: [
        {
          required: false,
          message: '请选择工艺',
        },
      ],
      hidden: false,
    },
    {
      name: 'fabricType',
      label: '面料',
      type: 'Input',
      placeholder: '请输入面料',
      config: {
        showCount: true,
        maxLength: 50,
      },
      rules: [
        {
          required: false,
          message: '请输入面料',
        },
      ],
      hidden: false,
    },
    {
      name: 'sellTempCode',
      label: '适卖温度',
      type: 'Select',
      option: FIT_HEAT_OPTIONS,
      placeholder: '请选择适卖温度',
      config: {
        showSearch: true,
        filterOption: true,
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: false,
          message: '请选择适卖温度',
        },
      ],
      hidden: false,
    },
    {
      name: 'gramWeight',
      label: '克重',
      type: 'InputNumber',
      placeholder: '请输入克重',
      config: {
        precision: 2,
        min: 0,
      },
      rules: [
        {
          required: false,
          message: '请输入克重',
        },
      ],
      hidden: false,
    },
    {
      name: 'priceTagCode',
      label: '价格带',
      type: 'Select',
      option: PRICE_BAND_OPTIONS,
      placeholder: '请选择价格带',
      config: {
        showSearch: true,
        filterOption: true,
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: false,
          message: '请选择价格带',
        },
      ],
      hidden: false,
    },
    {
      name: 'ageCode',
      label: '年龄段',
      type: 'Select',
      option: DICTDATA.age_level,
      placeholder: '请选择年龄段',
      config: {
        showSearch: true,
        filterOption: true,
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: false,
          message: '请选择年龄段',
        },
      ],
      hidden: false,
    },
  ]
  if (productTemplateVO) {
    originConfig.forEach((it) => {
      productTemplateVO.notRequired?.forEach((item) => {
        if (it.name === item) {
          if (it.rules) {
            it.rules[0].required = false
          }
        }
      })
      productTemplateVO.required?.forEach((item) => {
        if (it.name === item) {
          if (it.rules) {
            it.rules[0].required = true
          }
        }
      })
      productTemplateVO.notShow?.forEach((item) => {
        if (it.name === item) {
          it.hidden = true
        }
      })
    })
  }
  return originConfig
}
export const useFormCalc = ({ DICTDATA }: { DICTDATA: IDICTDATAType }): IItemArray[] => {
  return [
    {
      name: 'outPackageTypeCode',
      label: '外包方式',
      type: 'Select',
      option: DICTDATA.ectocyst_type,
      placeholder: '请选择外包方式',
      config: {
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: true,
          message: '请选择外包方式',
        },
      ],
    },
    {
      name: 'midPackageTypeCode',
      label: '中包标识',
      type: 'Select',
      option: DICTDATA.inbag_identity,
      placeholder: '请选择中包标识',
      config: {
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: true,
          message: '请选择中包标识',
        },
      ],
    },
    {
      name: 'packageTypeCode',
      label: '包装方式',
      type: 'Select',
      option: DICTDATA.pack_type,
      placeholder: '请选择包装方式',
      config: {
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: true,
          message: '请选择包装方式',
        },
      ],
    },
    {
      name: 'weightUnitCode',
      label: '计量单位',
      type: 'Select',
      option: DICTDATA.uom,
      placeholder: '请选择计量单位',
      config: {
        optionFilterProp: 'label',
      },
      rules: [
        {
          required: true,
          message: '请选择计量单位',
        },
      ],
    },
    {
      name: 'inPackageNum',
      label: '内箱数',
      type: 'InputNumber',
      placeholder: '请输入内箱数',
      config: {
        precision: 0,
        min: 0,
      },
      dependencies: ['outPackageNum'],
      rules: [
        ({ getFieldValue }: any) => ({
          validator: (_: any, value: number) => validateOutPackageNum(_, value, getFieldValue('outPackageNum')),
        }),
      ],
    },
    {
      name: 'outPackageNum',
      label: '外箱数',
      type: 'InputNumber',
      placeholder: '请输入外箱数',
      config: {
        precision: 0,
        min: 0,
      },
      dependencies: ['inPackageNum'],
      rules: [
        ({ getFieldValue }: any) => ({
          validator: (_: any, value: number) => validateOutPackageNum(_, getFieldValue('inPackageNum'), value),
        }),
      ],
    },
  ]
}
export const useFormUpload = ({ isuploadEdit }: { isuploadEdit: boolean }): IItemArray[] => {
  return [
    {
      name: 'mainPicFileId',
      label: '主图',
      config: { max: 1, disabled: !isuploadEdit, showPaste: true },
      type: 'Upload',
    },
    {
      name: 'otherPicFileIdList',
      label: '其他图片',
      config: { max: 3, disabled: !isuploadEdit, showPaste: true },
      type: 'Upload',
    },
  ]
}

export const useFormFabric = ({
  isEdit,
  handleFabricChange,
  FabricOption,
  handleSearchFabric,
  detailInfo,
}: {
  isEdit: boolean
  handleFabricChange: (_: number[], info: IOptionItem[]) => void
  FabricOption: IOptionItem[]
  handleSearchFabric: (newValue: string, devSeason: number, bigClassId: number[]) => void
  detailInfo?: Record<string, any>
}): IItemArray[] => {
  return [
    {
      name: 'fabricConfigIds',
      label: '面料',
      type: 'Select',
      option: FabricOption,
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        mode: 'multiple',
        filterOption: false,
        onSearch: (newValue: string) =>
          handleSearchFabric(
            newValue,
            detailInfo?.purchaseType === 1 ? '@' : detailInfo?.devSeasonName,
            detailInfo?.bigClassId,
          ),
        notFoundContent: <Empty description="请输入内容搜索" image={Empty.PRESENTED_IMAGE_SIMPLE} />,
        onChange: handleFabricChange,
        disabled: !isEdit,
      },
      hidden: !isEdit,
    },
  ]
}
export const useFormColor = ({
  isEdit,
  ColorOptions,
  handleSearchColor,
  SizeGroupOptions,
  setsizeRangeInfos,
  setskuInfo,
  skuInfo,
  iscolorEdit,
  handleClickAdd,
}: {
  isEdit: boolean
  ColorOptions: IOptionItem[]
  handleSearchColor: (newValue: string) => void
  SizeGroupOptions: IOptionItem[]
  setsizeRangeInfos: React.Dispatch<
    React.SetStateAction<
      {
        enableStatus: number
        label: string
        sizeDetailCode: string
        sizeDetailId: string
        sizeDetailName: string
        sizeGroupId: string
        sizeGroupName: string
        value: string
      }[]
    >
  >
  setskuInfo: React.Dispatch<React.SetStateAction<ISkuType>>
  skuInfo: ISkuType
  iscolorEdit: boolean
  handleClickAdd: () => void
}): IItemArray[] => {
  return [
    {
      name: 'skuInfoEditDTOS',
      label: '款式色',
      type: 'Select',
      option: ColorOptions,
      placeholder: '请选择款式色',
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        filterOption: false,
        onSearch: handleSearchColor,
        notFoundContent: <Empty description="请输入内容搜索" image={Empty.PRESENTED_IMAGE_SIMPLE} />,
        disabled: !isEdit,
        onChange: (
          _: string,
          info: { code: string; label: string; rgbValue: string; value: string; picUrl?: string },
        ) => {
          skuInfo.colorCode = info.code
          skuInfo.colorDetailName = info.label
          skuInfo.rgbValue = info.rgbValue
          skuInfo.enableStatus = 1
          skuInfo.picUrl = info?.picUrl
          setskuInfo(skuInfo)
        },
        optionRender: (option: any) => {
          return (
            <div style={{ display: 'flex', alignItems: 'center', padding: '0 12px' }}>
              {option?.data?.picUrl ? (
                <img
                  width={30}
                  height={30}
                  style={{ marginRight: 16, display: 'inline-block' }}
                  src={option?.data?.picUrl}
                />
              ) : (
                <div
                  style={{
                    width: 30,
                    height: 30,
                    display: 'inline-block',
                    verticalAlign: 'middle',
                    backgroundColor: `rgb${option?.data?.rgbValue}`,
                    marginRight: 16,
                  }}
                ></div>
              )}
              <div>{option?.label}</div>
            </div>
          )
        },
      },
      hidden: !isEdit,
    },
    {
      name: 'sizeRange',
      label: '尺码',
      type: 'Select',
      option: SizeGroupOptions,
      placeholder: '请选择尺码',
      config: {
        showSearch: true,
        optionFilterProp: 'label',
        disabled: !isEdit,
        onChange: (_: string, info: any) => {
          skuInfo.sizeInfo = info.label
          skuInfo.sizeCode = info.sizeDetailCode
          skuInfo.sizeId = info.sizeDetailId
          setskuInfo({ ...info, ...skuInfo })
        },
      },
      hidden: !isEdit,
    },
    {
      name: 'netWeight',
      label: '净重',
      type: 'InputNumber',
      config: {
        precision: 4,
        min: 0,
        onChange: (netWeight: number) => {
          setskuInfo({ ...skuInfo, netWeight })
        },
      },
    },
    {
      name: 'grossWeight',
      label: '毛重',
      type: 'InputNumber',
      config: {
        precision: 4,
        min: 0,
        onChange: (grossWeight: number) => {
          setskuInfo({ ...skuInfo, grossWeight })
        },
      },
    },
    {
      name: 'carryOverCode',
      label: '延续款',
      type: 'Input',
      config: {
        disabled: !isEdit,
        onChange: (event: any) => {
          skuInfo.carryOverCode = event.target.value
          setskuInfo(skuInfo)
        },
      },
      hidden: !isEdit,
    },
    {
      label: '',
      type: 'Custom',
      slot: (
        <Button
          type="primary"
          onClick={handleClickAdd}
          disabled={!iscolorEdit}
          style={{ display: !iscolorEdit ? 'none' : '' }}
          icon={<PlusOutlined />}
        >
          添加
        </Button>
      ),
    },
  ]
}
